package com.suse.service.view.edu.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.suse.common.config.response.R;
import com.suse.common.util.core.StringUtil;
import com.suse.model.dto.CourseDto;
import com.suse.service.view.edu.model.entity.Course;
import com.suse.service.view.edu.mapper.CourseMapper;
import com.suse.service.view.edu.model.entity.CourseCollect;
import com.suse.service.view.edu.model.param.CourseQueryParam;
import com.suse.service.view.edu.model.vo.ChapterVo;
import com.suse.service.view.edu.model.vo.CourseDetailVo;
import com.suse.service.view.edu.model.vo.MyCollectVo;
import com.suse.service.view.edu.service.ChapterService;
import com.suse.service.view.edu.service.CourseCollectService;
import com.suse.service.view.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-16
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private ChapterService chapterService;

    @Resource
    private CourseCollectService courseCollectService;

    @Override
    public R getCourseList(CourseQueryParam courseQueryParam) {
        // 获取查询条件
        String firstSubjectId = courseQueryParam.getFirstSubjectId();
        String secondSubjectId = courseQueryParam.getSecondSubjectId();
        String buyCountSort = courseQueryParam.getBuyCountSort();
        String priceSort = courseQueryParam.getPriceSort();
        String gmtCreateSort = courseQueryParam.getGmtCreateSort();
        // 进行条件查询
        List<Course> courseList = lambdaQuery()
                .select(Course::getId,
                        Course::getCover,
                        Course::getTitle,
                        Course::getViewCount,
                        Course::getBuyCount,
                        Course::getPrice)
                .eq(StringUtil.isNotEmpty(firstSubjectId), Course::getFirstSubjectId, firstSubjectId)
                .eq(StringUtil.isNotEmpty(secondSubjectId), Course::getSecondSubjectId, secondSubjectId)
                .orderByDesc(StringUtil.isNotEmpty(buyCountSort), Course::getBuyCount)
                .orderByDesc(StringUtil.isNotEmpty(priceSort), Course::getPrice)
                .orderByDesc(StringUtil.isNotEmpty(gmtCreateSort), Course::getGmtCreate)
                .list();
        return R.success(courseList);
    }

    @Override
    public R getCourseDetailById(String id) {
        CourseDetailVo courseDetailVo = courseMapper.getCourseDetailById(id);
        List<ChapterVo> chapterVoList = chapterService.getChapterList(id);
        courseDetailVo.setChapterList(chapterVoList);
        return R.success(courseDetailVo);
    }

    @Override
    public List<Course> getHotCourseList() {
        return lambdaQuery()
                .orderByDesc(Course::getBuyCount)
                .last("limit 8")
                .list();
    }

    @Override
    public CourseDto getCourseDtoById(String id) {
        return courseMapper.getCourseDtoById(id);
    }

    @Override
    public R hasCollect(String courseId,String userId) {
        Integer count = courseCollectService.lambdaQuery()
                .eq(CourseCollect::getCourseId, courseId)
                .eq(CourseCollect::getUserId, userId)
                .count();
        boolean hasCollect = count == 1;
        return R.put("hasCollect",hasCollect).success();
    }

    @Override
    public R getMyCollectList(String userId) {
        List<MyCollectVo> myCollectList = courseMapper.getMyCollectList(userId);
        return R.put("collectList",myCollectList).success();
    }

    @Override
    public List<Map<String, String>> getNameListByKey(String key) {
        List<Course> courseList = lambdaQuery().select(Course::getTitle)
                .likeRight(Course::getTitle, key)
                .list();
        if (CollectionUtil.isEmpty(courseList)) {
            return Collections.emptyList();
        }
        List<Map<String, String>> nameList = new ArrayList<>();
        for (Course course : courseList) {
            Map<String, String> map = new HashMap<>();
            map.put("value",course.getTitle());
            nameList.add(map);
        }
        return nameList;
    }

}
